package com.gitee.somenvm.ninerpgitem.core.command

import com.gitee.somenvm.ninerpgitem.compat.AttributeControl
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.command.CommandBody
import taboolib.common.platform.command.subCommand
import taboolib.common.util.isConsole
import taboolib.module.kether.KetherShell.eval
import taboolib.module.kether.ScriptOptions
import taboolib.module.nms.getItemTag
import taboolib.platform.util.hasLore

object TestCommand {
    @CommandBody
    val getItemAttrValue = subCommand {
        execute<ProxyCommandSender> { sender, context, a ->
            if (!sender.isConsole()) {
                val arg = a.replace("getItemAttrValue ", "")
                val player = sender.cast<Player>()
                if (player.inventory.itemInMainHand.hasLore()) {
                    val value = AttributeControl.attributeGetValue(player.inventory.itemInMainHand, arg)
                    sender.sendMessage("$arg value : $value")
                }
            }
        }
    }

    @CommandBody
    val getPlayerAttrValue = subCommand {
        execute<ProxyCommandSender> { sender, context, a ->
            if (!sender.isConsole()) {
                val player = sender.cast<LivingEntity>()
                val arg = a.replace("getPlayerAttrValue ", "")
                val value = AttributeControl.attributeGetValue(player, arg)
                sender.sendMessage("$arg value : $value")
            }
        }
    }

    @CommandBody
    val kether = subCommand {
        execute<ProxyCommandSender> { sender, c, a ->
            val arg = a.replace("kether ", "")
            sender.sendMessage("§1Input: §f $arg")
            sender.sendMessage("§1Input: c §f $c")

            val value = eval(arg, ScriptOptions.builder().namespace(emptyList()).sender(sender = sender).build()).get()
            //val ks = KetherShell.eval("$t", sender = context.sender()).toString()
            sender.sendMessage("§4KetherShell: §f$value")
        }
    }

    @CommandBody
    val nbt = subCommand {
        execute<ProxyPlayer> { sender, context, argument ->
            val arg = argument.replaceFirst("nbt ", "", true)
            val nbt = sender.cast<Player>().inventory.itemInMainHand.getItemTag().getDeep(arg)
            sender.sendMessage("as: ${nbt?.asString()} ")
            sender.sendMessage("to: ${nbt} ")
        }
    }

    @CommandBody
    val template = subCommand {

    }
}
